/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;


import io.renren.common.exception.RRException;
import io.renren.common.jnkt.JNADll;
import io.renren.common.jnkt.JnktSdkTest;
import io.renren.common.jnkt.MyCommandLineRunner;
import io.renren.common.utils.R;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * sdk测试接口
 *
 * @author macy
 */
@RestController
@RequestMapping("/sdk")
@Api(value = "sdk测试接口",description = "sdk测试接口")
public class SdkApiController {

    private static final Logger log = LoggerFactory.getLogger(SdkApiController.class);



    @GetMapping("open")
    @ApiOperation("开闸")
    public R open(@RequestParam(defaultValue = "10.192.84.220") String ip){

        clickIp(ip);

        int res=JnktSdkTest.KeyTop_OpenGate(ip);
        if(res!=0){
            return R.error("开闸失败");
        }

        return R.ok("开闸完成");
    }

    @GetMapping("compelOpenGate")
    @ApiOperation("常开")
    public R compelOpenGate(@RequestParam(defaultValue = "10.192.84.220") String ip){
        clickIp(ip);
        int res=JnktSdkTest.KeyTop_CompelOpenGate(ip);
        if(res!=0){
            return R.error("失败");
        }
        return R.ok("完成");
    }

    @GetMapping("revertGate")
    @ApiOperation("还原常开")
    public R revertGate(@RequestParam(defaultValue = "10.192.84.220") String ip){
        clickIp(ip);
        int res=JnktSdkTest.KeyTop_RevertGate(ip);
        if(res!=0){
            return R.error("失败");
        }
        return R.ok("完成");
    }


    @GetMapping("close")
    @ApiOperation("落闸")
    public R Close(@RequestParam(defaultValue = "10.192.84.220") String ip){

        clickIp(ip);
        int res=JnktSdkTest.KeyTop_CloseGate(ip);
        if(res!=0){
            return R.error("落闸失败");
        }
        return R.ok("落闸完成");
    }


    @GetMapping("capture")
    @ApiOperation("抓拍")
    public R capture(@RequestParam(defaultValue = "10.192.84.220") String ip){

        clickIp(ip);
        int res=JnktSdkTest.KeyTop_CaptureImage(ip);
        if(res!=0){
            return R.error("失败");
        }
        return R.ok("完成");
    }


    @GetMapping("queryStatus")
    @ApiOperation("查询地感信号")
    public R queryStatus(@RequestParam(defaultValue = "10.192.84.220") String ip){
        clickIp(ip);
        int res=JnktSdkTest.KeyTop_QueryStatus(ip);
        if(res!=0){
            return R.error("失败");
        }
        return R.ok("完成");
    }

    @GetMapping("getCamVersion")
    @ApiOperation("查询版本号")
    public R getCamVersion(@RequestParam(defaultValue = "10.192.84.220") String ip){
        clickIp(ip);
        return R.ok(JnktSdkTest.KeyTop_GetCamVersion(ip));
    }

    @GetMapping("rebootCamera")
    @ApiOperation("重启相机")
    public R rebootCamera(@RequestParam(defaultValue = "10.192.84.220") String ip){
        clickIp(ip);
        int res=JnktSdkTest.KeyTop_RebootCamera(ip);
        if(res!=0){
            return R.error("失败");
        }
        return R.ok("完成");
    }

    @RequestMapping("showtext")
    @ApiOperation("显示自定义文字")
    public R showtext(@RequestParam(defaultValue = "10.192.84.7") String ip,@RequestParam(defaultValue = "0600|09;1930|01;") String text){

        int res=JNADll.INSTANCE.ktparkNetLedShow(ip,text);
        if(res!=0){
            return R.error("失败");
        }
        return R.ok("完成");
    }




    @GetMapping("showArbitrary")
    @ApiOperation("按格式任意显示")
    public R showArbitrary(@RequestParam(defaultValue = "10.192.84.222") String ip){
        clickIp(ip);
        int res=JnktSdkTest.KeyTop_ShowArbitrary(ip);
        if(res!=0){
            return R.error("失败");
        }
        return R.ok("完成");
    }


    @GetMapping("showRemainPark")
    @ApiOperation("显示自定义文字")
    public R showRemainPark(@RequestParam(defaultValue = "10.192.84.222") String ip){
        clickIp(ip);
        int res=JnktSdkTest.KeyTop_ShowRemainPark(ip);
        if(res!=0){
            return R.error("失败");
        }
        return R.ok("完成");
    }





    @GetMapping("broadcast")
    @ApiOperation("播报语音")
    public R broadcast(@RequestParam(defaultValue = "10.192.84.222") String ip){

        clickIp(ip);
        int res=JnktSdkTest.KeyTop_Broadcast(ip);
        if(res!=0){
            return R.error("失败");
        }
        return R.ok("完成");
    }


    @GetMapping("showCarPlate")
    @ApiOperation("显示车牌")
    public R showCarPlate(@RequestParam(defaultValue = "10.192.84.222") String ip){

        clickIp(ip);
        int res=JnktSdkTest.KeyTop_ShowCarPlate(ip);
        if(res!=0){
            return R.error("失败");
        }
        return R.ok("完成");
    }




    private void clickIp(String ip) throws RRException {
        int inlineNum = JNADll.INSTANCE.getKTCAMinfo(MyCommandLineRunner.ipStr, MyCommandLineRunner.ipNum_max);

        log.debug("在线相机数量:"+inlineNum);

        if(inlineNum <=0){
            throw new RRException("没有在线的相机");
        }

        Map<String,String> map=JnktSdkTest.intToMap(MyCommandLineRunner.ipStr);
        if(!map.containsKey(ip)){
            throw new RRException("主机不在线");
        }
    }


}
